<?php
/**
 * Created by PhpStorm.
 * User: 尧
 * Date: 11/18/2016
 * Time: 2:10 PM
 */
namespace frontend\controllers;

use frontend\models\OrderForm;
use yii\web\Controller;

class ModelController extends Controller{

    /*public function actionOrder(){
        //实例化模型对象
        $orderForm = new OrderForm();
        //判断是否是post提交
        if (\Yii::$app->getRequest()->isPost) {
            //加载表单form数据，执行校验
            //获取post数据，并被模型载入
            $orderForm->load(\Yii::$app->getRequest()->post());
            $result = $orderForm->validate();
            var_dump($result);
            if (!$result) {
                var_dump($orderForm->getErrors());
            }

        } else {
            //分配到模板中
            return $this->render('order',['orderForm'=>$orderForm]);
        }

    }*/

    public function actionOrder(){
        //业务逻辑模型
        $orderForm = new orderForm();
        //判断是否是post提交
        if ($orderForm->load(\Yii::$app->getRequest()->post())) {
            //加载表单form数据，执行校验
            //利用模型完成订单生成结果页
            if ($order = $orderForm->checkout()) {
                //订单生成
                //重定向到订单生成结果页
                $this->redirect(['model/info', 'id'=>$order->order_id]);
            } else {
                $this->goBack();
            }
        }
            //分配到模板中
            return $this->render('order',['orderForm'=>$orderForm]);


    }
}